# JGSY.CMS.LowCode.Platform 单元测试运行脚本

Write-Host "=== JGSY.CMS.LowCode.Platform 控制器单元测试 ===" -ForegroundColor Green
Write-Host ""

# 检查 .NET SDK
Write-Host "检查 .NET SDK..." -ForegroundColor Yellow
$dotnetVersion = dotnet --version
Write-Host "当前 .NET SDK 版本: $dotnetVersion" -ForegroundColor Cyan
Write-Host ""

# 进入测试目录
$testProjectPath = "Tests\JGSY.CMS.LowCode.Platform.UnitTests"
if (-not (Test-Path $testProjectPath)) {
    Write-Host "错误: 测试项目目录不存在: $testProjectPath" -ForegroundColor Red
    exit 1
}

Set-Location $testProjectPath
Write-Host "已切换到测试目录: $(Get-Location)" -ForegroundColor Cyan
Write-Host ""

# 还原依赖包
Write-Host "还原 NuGet 包..." -ForegroundColor Yellow
dotnet restore
if ($LASTEXITCODE -ne 0) {
    Write-Host "错误: NuGet 包还原失败" -ForegroundColor Red
    exit 1
}
Write-Host "✅ NuGet 包还原完成" -ForegroundColor Green
Write-Host ""

# 编译项目
Write-Host "编译测试项目..." -ForegroundColor Yellow
dotnet build --no-restore
if ($LASTEXITCODE -ne 0) {
    Write-Host "错误: 编译失败" -ForegroundColor Red
    exit 1
}
Write-Host "✅ 编译成功" -ForegroundColor Green
Write-Host ""

# 运行测试分类

Write-Host "=== 1. 控制器可用性测试 ===" -ForegroundColor Magenta
dotnet test --filter "FullyQualifiedName~ControllerAvailabilityTests" --verbosity normal --no-build
Write-Host ""

Write-Host "=== 2. 系统控制器测试 ===" -ForegroundColor Magenta  
dotnet test --filter "FullyQualifiedName~SystemControllerTests" --verbosity normal --no-build
Write-Host ""

Write-Host "=== 3. 认证控制器测试 ===" -ForegroundColor Magenta
dotnet test --filter "FullyQualifiedName~AuthenticationControllerTests" --verbosity normal --no-build
Write-Host ""

Write-Host "=== 4. 用户控制器测试 ===" -ForegroundColor Magenta
dotnet test --filter "FullyQualifiedName~UsersControllerTests" --verbosity normal --no-build
Write-Host ""

Write-Host "=== 5. 文章控制器测试 ===" -ForegroundColor Magenta
dotnet test --filter "FullyQualifiedName~ArticlesControllerTests" --verbosity normal --no-build
Write-Host ""

Write-Host "=== 6. 其他控制器基础测试 ===" -ForegroundColor Magenta
dotnet test --filter "FullyQualifiedName~OtherControllersTests" --verbosity normal --no-build
Write-Host ""

# 运行所有测试
Write-Host "=== 运行所有单元测试 ===" -ForegroundColor Green
dotnet test --verbosity normal --no-build --logger "console;verbosity=detailed"

$testExitCode = $LASTEXITCODE
Write-Host ""

# 测试结果汇总
if ($testExitCode -eq 0) {
    Write-Host "🎉 所有测试通过!" -ForegroundColor Green
    Write-Host ""
    Write-Host "测试覆盖范围:" -ForegroundColor Cyan
    Write-Host "  ✅ 控制器基础架构测试" -ForegroundColor White
    Write-Host "  ✅ HTTP 路由和属性验证" -ForegroundColor White
    Write-Host "  ✅ 依赖注入配置检查" -ForegroundColor White
    Write-Host "  ✅ 授权和认证配置验证" -ForegroundColor White
    Write-Host "  ✅ 核心业务逻辑单元测试" -ForegroundColor White
    Write-Host "  ✅ 异常处理和错误响应测试" -ForegroundColor White
} else {
    Write-Host "❌ 测试失败，请查看上方详细信息" -ForegroundColor Red
}

Write-Host ""
Write-Host "测试完成时间: $(Get-Date)" -ForegroundColor Gray

# 返回根目录
Set-Location ..\..
